<?php

// UNDER CONSTRUCTION  , No code comments yet.

/**
 * Description of model_tags
 *
 * @author Steffen Bilde
 */
class Model_tags extends CI_Model
{
    function  __construct()
    {
        parent::__construct();
    }

	function build_tag_cloud(){
		$data = array();
		$sql = "SELECT tag,count(*) as ctr FROM `tags` group by tag order by ctr desc";
		$Q = $this->db->query($sql);

		if ($Q->num_rows() > 0){
			foreach ($Q->result() as $row){
				$data[] = array("tag" => $row->tag, "counter" => $row->ctr);
			}
		}
		$Q->free_result();
		shuffle($data);
		return $data;
	}


	function search_tags($input){
		$data = array();
                $term = $input; //Bilde
                
		$this->db->select('user_id');
		$this->db->like('tag', $term);
		$this->db->like('object', 'users');
		$Q = $this->db->get("tags");
		if ($Q->num_rows() > 0){
			foreach ($Q->result() as $row){
				$data[$row->user_id] = true;
			}
		}
		$Q->free_result();
		return $data;
	}


	function list_tags(){
		$userid = $this->session->userdata('id');
		$data = array();
		$this->db->select("tag");
		$this->db->order_by('tag','asc');
		$this->db->where("user_id",$userid);
		$Q = $this->db->get("tag_follows");
		if ($Q->num_rows() > 0){
			foreach ($Q->result() as $row){
				$data[] = $row->tag;
			}
		}


		$Q->free_result();
		return $data;

	}


	function list_all_tags(){
		$data = array();
		$this->db->select("tag");
		$this->db->order_by('tag','asc');
		$Q = $this->db->get("tags");
		if ($Q->num_rows() > 0){
			foreach ($Q->result() as $row){
				$temp[] = $row->tag;
			}
		}

		$data = array_unique($temp);
		$Q->free_result();
		return $data;

	}



	function check_tag_exists($tag){
		$this->db->select('id');
		$this->db->like('tag', $tag);
		$this->db->limit(1);
		$Q = $this->db->get("tags");
		if ($Q->num_rows() > 0){
			$row = $Q->row();
			return $row->id;
		}else{
			return 0;
		}

	}



	function follow_tag($tag){
		$userid = $this->session-userdata('id');
		$data = array(
                                'tag' => $tag, //Bilde
				'user_id' => $userid,
			);

		$this->db->insert("tag_follows",$data);
	}


	function unfollow_tag($tag){
		$userid = $this->session->userdata('id');
		$this->db->limit(1);
		$this->db->where('tag', $tag);
		$this->db->where('user_id', $userid);
		$this->db->delete('tag_follows');

	}


	function list_objects($tag){
		$data = array();

		$this->db->like("tag",$tag);
		$Q = $this->db->get("tags");
		if ($Q->num_rows() > 0){
			foreach ($Q->result() as $row){
				$data[] = $row;
			}
		}

		$Q->free_result();


		return $data;
	}

	function list_tag_users(){
		$data = array();
		$this->db->select("tag,object_id");
		$this->db->where("object","users");
		$Q = $this->db->get("tags");
		if ($Q->num_rows() > 0){
			foreach ($Q->result() as $row){
				$data[$row->object_id][] = $row->tag;
			}
		}

		$Q->free_result();
		return $data;

	}

	function list_tag_one_user($userid){
		$data = array();
		$this->db->select("tag");
		$this->db->where("object","users");
		$this->db->where("object_id",$userid);
		$Q = $this->db->get("tags");
		if ($Q->num_rows() > 0){
			foreach ($Q->result() as $row){
				$data[] = $row->tag;
			}
		}

		$Q->free_result();
		return $data;

	}

	function list_tag_objects($object){
		$userid = $this->session->userdata('id');
		$data = array();
		$this->db->select("tag,object_id");
		$this->db->where("user_id",$userid);
		$this->db->where("object",$object);
		$Q = $this->db->get("tags");
		if ($Q->num_rows() > 0){
			foreach ($Q->result() as $row){
				$data[$row->object_id][] = $row->tag;
			}
		}


		$Q->free_result();
		return $data;

	}

	function list_tag_objects_single($id,$object){
		//$userid = $_SESSION['userid'];   //Disse er udkommenteret fra start
		//$this->db->where("user_id",$userid);
		$data = array();
		$this->db->select("tag,object_id");
		$this->db->where("object",$object);
		$this->db->where("object_id",$id);
		$Q = $this->db->get("tags");
		if ($Q->num_rows() > 0){
			foreach ($Q->result() as $row){
				$data[$row->object_id][] = $row->tag;
			}
		}


		$Q->free_result();
		return $data;

	}

	function delete_tag($id){
		$this->db->limit(1);
		$this->db->where('id', $id);
		$this->db->delete('tags');

	}

//	function add_tags($tags,$object,$object_id){
//		$tags_array = explode(",",$tags);
//		if (!isset($this->session->userdata('id'))){
//			$userid = $object_id;
//		}else{
//			$userid = $this->session->userdata('id');
//		}
//		$now = date("Y-m-d h:i:s");
//
//
//		//first, remove all tags associated with object and object_id
//		$this->db->where('object', $object);
//		$this->db->where('object_id',$object_id);
//		$this->db->where('user_id',$userid);
//		$this->db->delete('tags');
//
//
//		//now add the new stuff
//		foreach ($tags_array as $tag){
//			$disallowed = array(" ", "/", "[", "]", "?", "(", ")" ,"{", "}" ,"=", "'", '"', ',' , ';', ':');
//			$cleantag = str_replace($disallowed,"",$tag);
//			$data = array(
//				'tag' => xss_clean($cleantag),
//				'object' => $object,
//				'object_id' => $object_id,
//				'user_id' => $userid,
//				'created' => $now
//			);
//
//			$this->db->insert("tags",$data);
//
//		}
//
//
//	}



}

?>
